﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Collections.Specialized;
using Silmoon;
using Silmoon.Threading;
using Silmoon.Windows.Controls.Extension;

namespace Cooky.Face
{
    public partial class FaceForm : Silmoon.Windows.Forms.GenieForm
    {
        GenieExtension ge = null;
        public const string version = "0.0.0.1";
        public FaceForm()
        {
            InitializeComponent();
            ge = new GenieExtension(this);
            CloseStyle = WindowCloseStyle.MaxStyleExt;
        }

        private void ctlExitButton_Click(object sender, EventArgs e)
        {
            
            panel1.Visible = false;
            Close();
        }

        private void ctlCheckMyDomainButton_Click(object sender, EventArgs e)
        {
            CheckDomainForm checkDomain = new CheckDomainForm();
            checkDomain.ShowEx();
            checkDomain.FormClosed += new FormClosedEventHandler(Form_FormClosed);
            hideThis();
        }

        void Form_FormClosed(object sender, FormClosedEventArgs e)
        {
            Application.Exit();
            //if (!this.Visible)
            //{
            //    panel1.Visible = true;
            //    this.ShowEx();
            //}
        }

        void hideThis()
        {
            panel1.Visible = false;
            HideEx();
        }


        private void FaceForm_Load(object sender, EventArgs e)
        {
            ShowEx();
            Threads.ExecAsync(checkUpdate, null);
        }

        protected override void OnFormClosing(FormClosingEventArgs e)
        {
            panel1.Visible = false;
            base.OnFormClosing(e);
        }

        private void ctlCallExe_CookyClientButton_Click(object sender, EventArgs e)
        {
            hideThis();
            Cooky.Client.ClientFaceForm client = new Cooky.Client.ClientFaceForm();
            client.FormClosed += new FormClosedEventHandler(Form_FormClosed);
            client.ShowLogin();
        }

        void checkUpdate()
        {
            ctlVersionLabel.Text = "Test version";
            return;
            WebClient wc = new WebClient();
            string s = wc.DownloadString("https://supportapp2.lab.dnspod.com/Programs/DNSPodCrystal/config.txt");
            NameValueCollection pars = SmString.AnalyzeNameValue(s.Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries), "=", "");
            this.Invoke(new tmpHander(delegate()
            {
                if (version == pars["version"])
                {
                    ctlVersionLabel.ForeColor = Color.Red;
                    ctlVersionLabel.Text = "有新版本 " + pars["version"];
                    ctlVersionLabel.Click += new EventHandler(delegate(object sender, EventArgs args)
                    {
                        MessageBox.Show("已经存在新版本的程序，到www.dnspod.cn客户端下载页面下载。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    });
                }
                else
                {
                    ctlVersionLabel.Text = "版本 " + version;
                }
            }));
        }

        private void FaceForm_Shown(object sender, EventArgs e)
        {
            ge.ShowControl(ctlCheckMyDomainButton, true, 0);
            ge.ShowControl(ctlCallExe_CookyClientButton, true, 200);
            ge.ShowControl(ctlExitButton, true, 400);
        }
    }
    delegate void tmpHander();
}